<?php
/** 
 * Copyright (C) 2009 João Neto
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * @version JLojaController.php - 19/07/2009 - joaoneto
 * @package lib/controller
 */
defined( '_JEXEC' ) or die( 'Restricted access' ); //no direct access

jimport( 'joomla.application.component.view' );

abstract class JLoja_View extends JView
{

	protected $_option;

	private $_jsPath = '';
	public function __construct( $config = array() )
	{
		parent::__construct( $config );
		$controller = JRequest::getVar ( 'controller', 'jloja' );
		$this->_jsPath = "/administrator/components/com_jloja/_lib/js/$controller/$controller.js";
		
		$this->_loadJavascriptLibraries();
		$this->_option = JRequest::getVar( 'option', '' );
		$page_name = JText::_( $this->_getTitle( ) );
		$build = $this->_getBuild();
		JToolBarHelper::title( JText::_( 'JLOJA_TITLE' ) . " <small><small> [{$page_name}] </small></small> <small style=\"font-size: 10px\">{$build}</small>" );
	}

	public function display( $tpl = null )
	{
		parent::display( $tpl );
		if( JFactory::getApplication()->getCfg( 'debug' ) )
		{
			echo '<div id="jloja-debug" class="jloja-debug"></div>';
		}
		echo 'JLoja_Debug:';
		var_dump( JLoja_Html_Debug::$debug );
		echo '<center><small>JLoja is Free Software released under the GNU/GPL License. </small></center>';
	}
	private function _getBuild()
	{
		return JLoja_DevInfo::getVersion();
	}

	private function _getTitle()
	{
		$title = $this->_title;

		if( !$title ){
			return 'Controller need a name!';
		}

		return $title;
	}

	private function _loadJavascriptLibraries()
	{
		$document = JFactory::getDocument();
		$document->addScript( JURI::root( true ) . '/administrator/components/com_jloja/_lib/js/jquery-1.3.2.min.js' );
		$document->addScript( JURI::root( true ) . '/administrator/components/com_jloja/_lib/js/jloja.js' );
		$document->addScript( JURI::root( true ) . $this->_jsPath );
		$document->addScript( JURI::root( true ) . '/administrator/components/com_jloja/_lib/js/jquery-ui-1.7.2.min.js' );
		$document->addStyleSheet( JURI::root( true ) . '/administrator/components/com_jloja/_lib/js/css/smoothness/jquery-ui-1.7.2.custom.css' );
		$document->addStyleSheet( JURI::root( true ) . '/administrator/components/com_jloja/_lib/js/css/jloja.css' );

		$viewName = get_class( $this );
		$debug = "if( console ) { console.info( exception ); }";
		if( JFactory::getApplication()->getCfg( 'debug' ) )
		{
			$debug = "JLojaDebug.addError( exception.message )";
		}
		
		$document->addScriptDeclaration(
		    "window.addEvent( 'domready', function(){
			jQuery.noConflict();
			try{
				new $viewName();
			}catch( exception ){
				$debug
			}
		    })"
		);
	}
}